<?php

class UsersModel extends CI_Model {
	
	function select_all()
	{
		$this->load->database();
		$query = $this->db->query('SELECT * FROM user');

		return $query->result();
	}
	
	function select_all_by_order()
	{
		$this->load->database();
		$query = $this->db->query('SELECT * FROM id_emp ORDER BY emp_number');

		return $query->result();
	}
	
	function select_user($id)
	{
		$this->load->database();
		$query = $this->db->get_where('user', array('user_id' =>  $id));
		
		return $query->row();
	}
	
    function insert_data()
    {
		$this->load->database();
		$post_bday = $_POST['user_birthday'];
		$bday = date("Y-m-d", strtotime($post_bday));
        $this->user_firstname		= $_POST['user_firstname']; 
        $this->user_middlename		= $_POST['user_middlename']; 
        $this->user_lastname 		= $_POST['user_lastname']; 
		$this->user_gender			= $_POST['user_gender'];
        $this->user_email			= $_POST['user_email']; 
        $this->user_cellphone 		= $_POST['user_cellphone']; 
        $this->user_birthday 		= $bday; 
        $this->user_address 		= $_POST['user_address']; 
        $this->user_city 			= $_POST['user_city']; 
        $this->user_hometown 		= $_POST['user_hometown'];
        $this->user_country			= $_POST['user_country'];   
		
		$this->db->insert('user', $this);
    }
	
	function update_data()
	{
		$this->load->database();
		$pre_bday = $_POST['user_birthday'];
		$bday = date("Y-m-d", strtotime($pre_bday));
        $this->user_firstname		= $_POST['user_firstname']; 
        $this->user_middlename		= $_POST['user_middlename']; 
        $this->user_lastname 		= $_POST['user_lastname']; 
		$this->user_gender			= $_POST['user_gender'];
        $this->user_email			= $_POST['user_email'];
        $this->user_cellphone 		= $_POST['user_cellphone'];
        $this->user_birthday 		= $bday;
        $this->user_address 		= $_POST['user_address'];
        $this->user_city 			= $_POST['user_city'];
        $this->user_hometown 		= $_POST['user_hometown'];
        $this->user_country			= $_POST['user_country'];
		
		$this->db->where('user_id', $_POST['user_id']);
		$this->db->update('user', $this);
	}
	
	function insert_photo()
	{
		$this->load->database();
		
		$fileName = $_FILES['userfile']['name'];
		$tmpName = $_FILES['userfile']['tmp_name'];
		$fileSize = $_FILES['userfile']['size'];
		$fileType = $_FILES['userfile']['type'];
		$fp      = fopen($tmpName, 'r');
		$content = fread($fp, filesize($tmpName));
		
		 fclose($fp);
		 if(!get_magic_quotes_gpc())
		 {
			$fileName = addslashes($fileName);
		 }
			$this->photo = $content;
			$this->user_id = $_POST['user_id'];
			$this->db->where('user_id', $_POST['user_id']);
			$this->db->insert('photo', $this);
	}
	
	function delete_data()
	{
		$this->load->database();
		$this->db->query('DELETE FROM id_emp WHERE user_id=' . $_GET['id'] . '');
		
	}
	
	function get_employee($employee_id)
	{
		$this->load->database();
		return $this->db->get_where('id_emp', array('employee_number' =>  $emp_id));
	}
	
}

?>
